package msr.frontend;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import msr.object.Capacity;
import msr.object.Jichiku;
import msr.service.OracleService;
import msr.util.DateParse;
import msr.util.Define;
import msr.util.RequestAction;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EditAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	OracleService oService = new OracleService();
	private static final Logger log = Logger.getLogger(EditAction.class
			.getName());

	private int seqNo;
	private String feedCD;
	private String feedName;
	private String kibouYMD;
	private String formCD;
	private String formName;
	private int qty;
	private int weight;
	private String getCD;
	private String kubunFaYoyaku;
	private String freeComment;
	private String jichikuCD;
	private List<Capacity> listCapacity;
	private List<Jichiku> listJichiku;
	private String actionType;// delete or update
	private String productexpect_year;
	private String productexpect_month;
	private String productexpect_day;
	private boolean overDate;
	private List<String> listYear;
	private List<String> listMonth;
	private List<String> listDay;
	private int _year;
	private int _month;
	private int _day;
	private String type;
	/**
	 * 
	 */
	Map<String, Object> session = ActionContext.getContext().getSession();
	private String username;
	private String passwd;

	public String execute() {
		this.getListTime();
		this.setValueOfTime(kibouYMD);
		DateParse dParse = new DateParse();
		String now = dParse.getNow();
		String kibou = dParse.invertParseDate(kibouYMD);
		System.out.println("value of kibou" + kibou);
		overDate = dParse.compareDate(now, kibou);
		type = oService.checkType(formCD);
		this.setUsername((String) session.get("username"));
		this.setPasswd((String) session.get("passwd"));

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", (String) session.get("username"));
		// get list capacity
		String productCapacity = oService.getCapacity(feedCD);
		listCapacity = oService.getListCapacity(productCapacity);
		// get list jichiku
		listJichiku = oService.getListJichiku(params,
				RequestAction.GET_ALL_JICHIKU_BY_USER);
		System.out.println("size of list capacity " + listCapacity.size());
		System.out.println("size of list jichiku " + listJichiku.size());
		if (overDate) {
			addActionError(getText("error.overDate"));
			return ERROR;
		}
		return SUCCESS;
	}

	public String editYoyaku() {
		log.info("update data for seqNo " + seqNo);
		log.info("update data for formcd " + formCD);
		Map<String, Object> params = new HashMap<String, Object>(); // put data
																	// into map
		String productdate = new DateParse().parseDate(productexpect_year,
				productexpect_month, productexpect_day);
		System.out.println("value of productdate " + productdate);

		params.put("seqNo", seqNo);
		params.put("weight", weight);
		params.put("qty", qty);
		params.put("formCD", formCD);
		// params.put("kibouYMD", kibouYMD);
		params.put("kibouYMD", productdate);
		params.put("jichikuCD", jichikuCD);
		params.put("freeComment", freeComment);
		params.put("kubunFaYoyaku", Define.EDIT);// because it in edit mode
		java.util.Date utilDate = new java.util.Date();
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
		params.put("uptime", sqlDate);
		params.put("shori_flg", Define.UNRESOLVE);

		boolean check = oService.editYoyaku(params,
				RequestAction.UPDATE_YOYAKU_FULL);
		if (check)
			return SUCCESS;
		else
			return ERROR;
	}

	public String deleteYoyaku() {
		log.info("予約削除実行中。。。");
		log.info("value of seqNo " + seqNo);
		Map<String, Object> params = new HashMap<String, Object>(); // put data
		// into map
		params.put("seqNo", seqNo);
		params.put("kubunFaYoyaku", "03");// because it in edit mode
		java.util.Date utilDate = new java.util.Date();
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
		params.put("uptime", sqlDate);
		boolean check = oService.deleteYoyaku(params);
		if (!check) {
			addActionError(getText("error.deleteError"));
			return ERROR;
		}
		addActionMessage("msg.deletesuccess");
		return SUCCESS;
	}

	public void setValueOfTime(String orgDate) {
		if (orgDate != null) {
			_day = Integer.parseInt(orgDate.substring(8, 10));
			_month = Integer.parseInt(orgDate.substring(5, 7));
			_year = Integer.parseInt(orgDate.substring(0, 4));
		}
	}

	public void getListTime() {
		Calendar cal = Calendar.getInstance();
		System.out.println("yea " + cal.get(Calendar.YEAR));
		listYear = new ArrayList<String>();
		listMonth = new ArrayList<String>();
		listDay = new ArrayList<String>();

		listYear.add(String.valueOf(cal.get(Calendar.YEAR)));
		listYear.add(String.valueOf(cal.get(Calendar.YEAR) - 1));
		for (int i = 1; i < 13; i++) {
			listMonth.add(String.valueOf(i));
		}
		for (int i = 1; i <= 31; i++) {
			listDay.add(String.valueOf(i));
		}

	}

	public int getSeqNo() {
		return seqNo;
	}

	public void setSeqNo(int seqNo) {
		this.seqNo = seqNo;
	}

	public String getFeedCD() {
		return feedCD;
	}

	public void setFeedCD(String feedCD) {
		this.feedCD = feedCD;
	}

	public String getFeedName() {
		return feedName;
	}

	public void setFeedName(String feedName) {
		this.feedName = feedName;
	}

	public String getFormCD() {
		return formCD;
	}

	public void setFormCD(String formCD) {
		this.formCD = formCD;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public String getGetCD() {
		return getCD;
	}

	public void setGetCD(String getCD) {
		this.getCD = getCD;
	}

	public String getFreeComment() {
		return freeComment;
	}

	public void setFreeComment(String freeComment) {
		this.freeComment = freeComment;
	}

	public List<Capacity> getListCapacity() {
		return listCapacity;
	}

	public void setListCapacity(List<Capacity> listCapacity) {
		this.listCapacity = listCapacity;
	}

	public List<Jichiku> getListJichiku() {
		return listJichiku;
	}

	public void setListJichiku(List<Jichiku> listJichiku) {
		this.listJichiku = listJichiku;
	}

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getKubunFaYoyaku() {
		return kubunFaYoyaku;
	}

	public void setKubunFaYoyaku(String kubunFaYoyaku) {
		this.kubunFaYoyaku = kubunFaYoyaku;
	}

	public String getKibouYMD() {
		return kibouYMD;
	}

	public void setKibouYMD(String kibouYMD) {
		this.kibouYMD = kibouYMD;
	}

	public String getJichikuCD() {
		return jichikuCD;
	}

	public void setJichikuCD(String jichikuCD) {
		this.jichikuCD = jichikuCD;
	}

	public boolean isOverDate() {
		return overDate;
	}

	public void setOverDate(boolean overDate) {
		this.overDate = overDate;
	}

	public String getProductexpect_year() {
		return productexpect_year;
	}

	public void setProductexpect_year(String productexpect_year) {
		this.productexpect_year = productexpect_year;
	}

	public String getProductexpect_month() {
		return productexpect_month;
	}

	public void setProductexpect_month(String productexpect_month) {
		this.productexpect_month = productexpect_month;
	}

	public String getProductexpect_day() {
		return productexpect_day;
	}

	public void setProductexpect_day(String productexpect_day) {
		this.productexpect_day = productexpect_day;
	}

	public List<String> getListYear() {
		return listYear;
	}

	public void setListYear(List<String> listYear) {
		this.listYear = listYear;
	}

	public List<String> getListMonth() {
		return listMonth;
	}

	public void setListMonth(List<String> listMonth) {
		this.listMonth = listMonth;
	}

	public List<String> getListDay() {
		return listDay;
	}

	public void setListDay(List<String> listDay) {
		this.listDay = listDay;
	}

	public int get_year() {
		return _year;
	}

	public void set_year(int _year) {
		this._year = _year;
	}

	public int get_month() {
		return _month;
	}

	public void set_month(int _month) {
		this._month = _month;
	}

	public int get_day() {
		return _day;
	}

	public void set_day(int _day) {
		this._day = _day;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
